package com.itheima.reggie.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;



    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "SetmealCache", allEntries = true)
    public R<String> saveSetmeal(@RequestBody SetmealDto setmealDto) {
        log.info("setmealDto: {}", setmealDto);
        setmealService.saveSetmealDish(setmealDto);
        return R.success("套餐添加成功");
    }


    /**
     * 套餐分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageSetmeals(int page,int pageSize,String name) {
        //分页构造器
        Page<Setmeal> pageSetmeal = new Page<>(page,pageSize);
        //缺少套餐分类，使用新Page
        Page<SetmealDto> dtoPage = new Page<>();
        //数据管理
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //根据name查询
        queryWrapper.like(name!=null,Setmeal::getName,name);
       //根据更新时间排序
       queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageSetmeal,queryWrapper);

        //将查询出的信息复制到dtoPage
        BeanUtils.copyProperties(pageSetmeal,dtoPage,"records");

        List<Setmeal> records = pageSetmeal.getRecords();
        List<SetmealDto> dtoRecords = records.stream().map((item)->{
            SetmealDto dto = new SetmealDto();
            //将信息copy
            BeanUtils.copyProperties(item,dto);
            //根据id获取分类名称
            Category category = categoryService.getById(item.getCategoryId());
            if(category != null){
                dto.setCategoryName(category.getName());
            }else{
                dto.setCategoryName("无");
            }
            return dto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(dtoRecords);
        return R.success(dtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "SetmealCache", allEntries = true)
    public R<String> deleteSetmeal(@RequestParam List<Long> ids){
        log.info("id是{}",ids);
        setmealService.deleteSetmealDish(ids);
        return R.success("套餐删除成功");
    }


    /**
     * 前端套餐显示
     * @param setmeal
     * @return
     */
    @Cacheable(value = "SetmealCache",key="#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("list")
    public R<List<Setmeal>> listSetmeal(Setmeal setmeal) {//通过url地址传输
        //查询条件
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());

        List<Setmeal> setmealList =  setmealService.list(queryWrapper);

        return R.success(setmealList);
    }

    /**
     * 套餐停售||起售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<List<Setmeal>> updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids){
        log.info("Id {}", ids);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        List<Setmeal> SetmealList = setmealService.list(queryWrapper);
        SetmealList.stream().map((item)->{
            item.setStatus(status);
            return item;
        }).collect(Collectors.toList());

        setmealService.updateBatchById(SetmealList);

        return R.success(SetmealList);
    }

}
